{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "84e6656d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import random"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3ed48cc4",
   "metadata": {},
   "source": [
    "# $k$-step transition probability\n",
    "## 1. by sampling"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a1b2d0b8",
   "metadata": {},
   "outputs": [],
   "source": [
    "def update_state(s):\n",
    "    \"\"\"\n",
    "        Does one step of the Markov chain. \n",
    "    \"\"\"\n",
    "    \n",
    "    if s==0:\n",
    "        if random.random() < 0.99: return 0\n",
    "        else: return 1\n",
    "    elif s==1:\n",
    "        if random.random() < 0.9: return 0\n",
    "        else: return 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "31449c13",
   "metadata": {},
   "outputs": [],
   "source": [
    "def many_updates(s,k):\n",
    "    for _ in range(k):\n",
    "        s = update_state(s)\n",
    "        \n",
    "    return s"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "789b54e2-01d2-4747-a0c4-a64cd2e264c8",
   "metadata": {},
   "source": [
    "First, we sample $k$ steps of the Markov chain. We do this $10^5$ times and thus sample the probability distribution at the $k$-th step. \n",
    "We compare two case: one where we start with $s=0$ and another starting with $s=1$. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "1f4b042f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.98883 0.01117\n",
      "0.98904 0.01096\n"
     ]
    }
   ],
   "source": [
    "n = 10**5\n",
    "L = [many_updates(s=0,k=100) for _ in range(n)]\n",
    "\n",
    "print(L.count(0)/n, L.count(1)/n)\n",
    "\n",
    "L = [many_updates(s=1,k=100) for _ in range(n)]\n",
    "\n",
    "print(L.count(0)/n, L.count(1)/n)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ba7c058b",
   "metadata": {},
   "source": [
    "## 2. By Chapman-Kolmogorov formula"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "98dd75bf-e5bc-4f15-a1f6-34ea12db21f6",
   "metadata": {},
   "source": [
    "Next, we compute the probability exactly using formula from class: by computing powers of the matrix. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "5729abf5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[0.99, 0.01],\n",
       "        [0.9 , 0.1 ]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "P = np.matrix([[0.99, .01], [.9, .1]]); P"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "7bb226a3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[0.9891, 0.0109],\n",
       "        [0.981 , 0.019 ]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "P*P"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "faeff6a4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[0.98901099, 0.01098901],\n",
       "        [0.98901099, 0.01098901]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "P**100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b125e3db-ce4b-4b1d-a622-537de75ff8fa",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "state": {},
    "version_major": 2,
    "version_minor": 0
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
